package com.hs.activation.controller;
import com.alibaba.fastjson.JSONObject;
import com.hs.activation.common.annotation.PassToken;
import com.hs.activation.common.result.BaseController;
import com.hs.activation.common.result.Result;
import com.hs.activation.common.utils.WebUtils;
import com.hs.activation.entity.WxUserEntity;
import com.hs.activation.entity.menuEntity;
import com.hs.activation.service.IWxUserService;
import com.hs.activation.service.ImenuService;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.json.JSONParser;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

@RestController
@RequestMapping({"WxUser"})
public class WxUserController
        extends BaseController {
    @Resource
    IWxUserService service;
    @Resource
    ImenuService imenuService;

    @GetMapping({"/{id}"})
    @PassToken
    public Result queryList(@PathVariable Long id) {
        try {
            List<WxUserEntity> result = this.service.queryList(id);
            return new Result("操作成功", Integer.valueOf(200), result);
        } catch (Exception ee) {
            return new Result(ee.getMessage(), Integer.valueOf(400));
        }
    }

    @PostMapping({""})
    @PassToken
    public Result save(@RequestBody WxUserEntity entity) {
        boolean result = false;
        if (entity.getId() == null) {
            WxUserEntity user = this.service.findByOpenId(entity.getOpenid());
            if (user != null) {
                user.setActivationid(entity.getActivationid());
                this.service.update(user);
            } else {
                result = this.service.save(entity);
            }
        } else {
            result = this.service.update(entity);
        }
        if (!result) {
            return new Result("操作失败", Integer.valueOf(400));
        }
        return new Result("操作成功", Integer.valueOf(200));
    }

    @GetMapping({"/cheakuser/{openid}"})
    @PassToken
    public Result cheakUser(@PathVariable String openid) {
        WxUserEntity user = this.service.findByOpenId(openid);
        if (user != null) {
            List<menuEntity> menu = this.imenuService.queryList(user.getActivationid());
            return new Result("操作成功", Integer.valueOf(200), menu);
        }
        return new Result("请先扫描二维码绑定", Integer.valueOf(200));
    }

    @GetMapping({"/find/{openid}"})
    @PassToken
    public Result findUser(@PathVariable String openid) {
        WxUserEntity user = this.service.findByOpenId(openid);
        if (user != null) {
            return new Result("查询成功", Integer.valueOf(200), user);
        }
        return new Result("查询成功", Integer.valueOf(200), null);
    }

    @GetMapping({"delete/{id}"})
    @PassToken
    public Result deleteById(@PathVariable Long id) {
        boolean result = this.service.removeById(id);
        if (result == true) {
            return new Result("删除成功", Integer.valueOf(200));
        }
        return fail("操作失败");
    }

    @GetMapping({"/getopenid/{code}"})
    @PassToken
    public Result getopenid(@PathVariable String code, HttpServletRequest request, HttpServletResponse response) {
        try {
            String appid = "wxe80748dd88b24081";
            String secret = "c4a1f4db4c7349236dc83919cd54b5e2";
            response.setHeader("Access-Control-Allow-Origin", "*");

            response.setHeader("Access-Control-Allow-Methods", "GET,POST");
            String wxLoginUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
            String param = "appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
            String jsonString = WebUtils.sendGet(wxLoginUrl, param);
            return new Result("获取成功", Integer.valueOf(200), jsonString);
        } catch (Exception ee) {
            return fail(ee.getMessage());
        }
    }
}
